package com.minshenglife.zanzan.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.dao.WorkConDao;
import com.minshenglife.zanzan.dao.WorkConMarketPowerDao;
import com.minshenglife.zanzan.entity.WorkCon;
import com.minshenglife.zanzan.pub.CalcUtils;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.result.WorkConResult;
import com.minshenglife.zanzan.service.WorkConService;

/**
 * 工作内容service
 * 
 * @author huodd
 * 
 */
@Component
@Transactional
public class WorkConServiceImpl implements WorkConService {
	Logger log = LoggerFactory.getLogger(WorkConServiceImpl.class);
	@Resource
	WorkConDao workConDao;
	@Resource
	WorkConMarketPowerDao workConMarketPowerDao;

	@Override
	@Transactional(readOnly = true)
	public WorkCon get(Long logId) {
		return workConDao.get(logId);
	}

	@Override
	public void addOrUpdate(WorkCon con) {
		// 通过日志id的有无，判定是否存在日志
		if (con.getId() != null && !"".equals(con.getId())) {
			workConDao.update(con);
		} else {
			workConDao.add(con);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public Page<WorkConResult> findWorkConList(String empId, String workConType, int pageNo, int pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("empId", empId);
		if(workConType!= null && workConType.equals("3")){
			workConType = "1";
		}
		map.put("workConType", workConType);
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countByParams(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConList(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	@Transactional(readOnly = true)
	public Page<WorkConResult> findWorkConViewList(String canBeReviewUser, int pageNo, int pageSize, String workConType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("canBeReviewUser", canBeReviewUser);
		map.put("workConType", workConType);
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countWorkConViewList(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConViewList(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public void updateWorkCon(WorkCon con) {
		workConDao.update(con);
	}

	@Override
	public Page<WorkConResult> findWorkConViewListOfDow(String empId, int pageNo, int pageSize, String searchArea, String searchPost, String searchEmp, String specialEmpId, String workConType) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (empId != null && !empId.equals("")) {
			map.put("empId", empId);
		}

		map.put("workConType", workConType);
		map.put("specialEmpId", specialEmpId);
		if (searchArea != null && !searchArea.equals("")) {
			if (searchPost.equals("S")) {
			} else {
				map.put("searchArea", searchArea);
			}
		}
		if (searchPost != null && !searchPost.equals("")) {
			if (searchPost.equals("S")) {//如果是S级别的可以看到全部的
			} else {
				map.put("searchPost", searchPost);
			}
		}
		if (searchEmp != null && !searchEmp.equals("")) {
			map.put("searchEmp", searchEmp);
		}
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countWorkConViewListOfDow(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConViewListOfDow(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public void workConShare(String id, boolean isShare) {
		log.info("start workConShare");
		WorkCon con = workConDao.get(Long.parseLong(id));
		con.setLog_date(con.getLog_date().substring(0, 10));
		if (isShare) {
			con.setIsShare(Keys.WORK_CONTENT_ISSHARE_YES);
		} else {
			con.setIsShare(Keys.WORK_CONTENT_ISSHARE_NO);
		}
		workConDao.update(con);
		log.info("end workConShare");
	}

	@Override
	public Page<WorkConResult> findWorkShareList(String workConType, int pageNo, int pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShare", Keys.WORK_CONTENT_ISSHARE_YES);
		map.put("workConType", workConType);
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countByParams(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConList(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public Page<WorkConResult> WCSpecialCareList(String empId, String workConType, int pageNo, int pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("empId", empId);
		map.put("workConType", workConType);
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countWorkConSpecialCareList(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConSpecialCareList(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public List<WorkCon> findWorkConListOfDate(String canBeReviewUser, String workConType, String logDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("canBeReviewUser", canBeReviewUser);
		map.put("workConType", workConType);
		map.put("logDate", logDate);
		return workConDao.findWorkConListOfDate(map);
	}

	public static void main(String[] args) {
		String d = "2015-03-06 00:00:00";
		System.out.println(d.substring(0, 10));
	}

	@Override
	public Page<WorkConResult> findWorkConViewListOfDow(String empId,
			int pageNo, int pageSize, String searchArea, String searchPost,
			String searchEmp, String specialEmpId, String workConType,
			String searchEmpName, String searchConState,
			String searchConStartTime, String searchConEndTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (empId != null && !empId.equals("")) {
			map.put("empId", empId);
		}
		map.put("workConType", workConType);
		map.put("specialEmpId", specialEmpId);
		if (searchArea != null && !searchArea.equals("")) {
//			if (searchPost.equals("S")) {
//			} else {
				map.put("searchArea", searchArea);
//			}
		}
		if (searchPost != null && !searchPost.equals("")) {
//			if (searchPost.equals("S")) {//如果是S级别的可以看到全部的
//			} else {
				map.put("searchPost", searchPost.split(","));
//			}
		}
		if (searchEmp != null && !searchEmp.equals("")) {
			map.put("searchEmp", searchEmp);
		}
		if (searchEmpName != null && !searchEmpName.equals("")) {
			map.put("searchEmpName", searchEmpName);
		}
		if (searchConState != null && !searchConState.equals("")) {
			map.put("searchConState", searchConState);
		}
		if (searchConStartTime != null && !searchConStartTime.equals("")) {
			map.put("searchConStartTime", searchConStartTime);
		}
		if (searchConEndTime != null && !searchConEndTime.equals("")) {
			map.put("searchConEndTime", searchConEndTime);
		}
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countWorkConViewListOfDow(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConViewListOfDow(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public Page<WorkConResult> findWorkConMarketViewListOfDow(String empId,String specialEmpId,
			int pageNo, int pageSize, String workConType) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (empId != null && !empId.equals("")) {
			map.put("empId", empId);
		}
		if(workConType != null && workConType.equals("3")){
			workConType = "1";
		}
		map.put("workConType", workConType);
		map.put("specialEmpId", specialEmpId);
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countWorkConMarketViewListOfDow(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConMarketViewListOfDow(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public Page<WorkConResult> findWorkConViewList(String canBeReviewUser,
			int pageNo, int pageSize, String workConType, String searchEmpName,
			String searchConState, String searchConStartTime,
			String searchConEndTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("canBeReviewUser", canBeReviewUser);
		map.put("workConType", workConType);
		if (searchEmpName != null && !searchEmpName.equals("")) {
			map.put("searchEmpName", searchEmpName);
		}
		if (searchConState != null && !searchConState.equals("")) {
			map.put("searchConState", searchConState);
		}
		if (searchConStartTime != null && !searchConStartTime.equals("")) {
			map.put("searchConStartTime", searchConStartTime);
		}
		if (searchConEndTime != null && !searchConEndTime.equals("")) {
			map.put("searchConEndTime", searchConEndTime);
		}
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countWorkConViewList(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConViewList(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public Page<WorkConResult> findWorkConMarketViewListOfDow(String empId,
			String specialEmpId, int pageNo, int pageSize, String workConType,
			String searchEmpName, String searchConState,
			String searchConStartTime, String searchConEndTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (empId != null && !empId.equals("")) {
			map.put("empId", empId);
		}
		if(workConType != null && workConType.equals("3")){
			workConType = "1";
		}
		map.put("workConType", workConType);
		map.put("specialEmpId", specialEmpId);
		if (searchEmpName != null && !searchEmpName.equals("")) {
			map.put("searchEmpName", searchEmpName);
		}
		if (searchConState != null && !searchConState.equals("")) {
			map.put("searchConState", searchConState);
		}
		if (searchConStartTime != null && !searchConStartTime.equals("")) {
			map.put("searchConStartTime", searchConStartTime);
		}
		if (searchConEndTime != null && !searchConEndTime.equals("")) {
			map.put("searchConEndTime", searchConEndTime);
		}
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countWorkConMarketViewListOfDow(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConMarketViewListOfDow(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public Page<WorkConResult> findWorkConViewListOfSearch(String empId,
			int pageNo, int pageSize, String searchArea, String searchPost,
			String searchEmp, String specialEmpId, String workConType,
			String searchEmpName, String searchConState,
			String searchConStartTime, String searchConEndTime) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (empId != null && !empId.equals("")) {
			map.put("empId", empId);
		}
		map.put("workConType", workConType);
		map.put("specialEmpId", specialEmpId);
		if (searchArea != null && !searchArea.equals("")) {
//			if (searchPost.equals("S")) {
//			} else {
				map.put("searchArea", searchArea);
//			}
		}
		if (searchPost != null && !searchPost.equals("")) {
//			if (searchPost.equals("S")) {//如果是S级别的可以看到全部的
//			} else {
				map.put("searchPost", searchPost);
//			}
		}
		if (searchEmp != null && !searchEmp.equals("")) {
			map.put("searchEmp", searchEmp);
		}
		if (searchEmpName != null && !searchEmpName.equals("")) {
			map.put("searchEmpName", searchEmpName);
		}
		if (searchConState != null && !searchConState.equals("")) {
			map.put("searchConState", searchConState);
		}
		if (searchConStartTime != null && !searchConStartTime.equals("")) {
			map.put("searchConStartTime", searchConStartTime);
		}
		if (searchConEndTime != null && !searchConEndTime.equals("")) {
			map.put("searchConEndTime", searchConEndTime);
		}
		int offset = (pageNo - 1) * pageSize;
		Long count = workConDao.countWorkConViewListOfSearch(map);
//		Long count = workConDao.countWorkConViewListOfDow(map);
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<WorkConResult> rows = workConDao.findWorkConViewListOfSearch(map);
//		List<WorkConResult> rows = workConDao.findWorkConViewListOfDow(map);
		Page<WorkConResult> epr = new Page<WorkConResult>();
		epr.setRows(rows);
		epr.setTotalRow(count);
		epr.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		epr.setPageSize(pageSize);
		epr.setPageNo(pageNo);
		return epr;
	}

	@Override
	public void updateWorkCon(Map<String, Object> map) {
		workConDao.updateReviewAndComment(map);
		
	}
}
